package org.springblade.modules.admin.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springblade.common.cache.DictBizCache;
import org.springblade.common.cache.DictCache;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.secure.utils.AuthUtil;
import org.springblade.core.tenant.annotation.TenantIgnore;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.constant.BladeConstant;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.TaskPlanPageQueryDTO;
import org.springblade.modules.admin.dto.TaskPlanSaveDTO;
import org.springblade.modules.admin.entity.MaintainTaskPlan;
import org.springblade.modules.admin.service.MaintainTaskPlanService;
import org.springblade.modules.admin.vo.TaskPlanVO;
import org.springblade.modules.system.entity.Dict;
import org.springblade.modules.system.entity.DictBiz;
import org.springblade.modules.system.entity.Tenant;
import org.springblade.modules.system.service.ITenantService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * @Date 9:36 2022/3/10
 * @Description {
 *    任务计划接口
 * }
 * @Author lsay
 **/
@RestController
@RequestMapping("/maintain/task/plan")
public class MaintainTaskPlanController {

	@Autowired
	private MaintainTaskPlanService taskPlanService;
	@Autowired
	private ITenantService tenantService;

	/**
	 * 任务计划分页
	 * @param pageQuery 查询条件
	 * @param query 分页条件
	 * @return
	 */
	@TenantIgnore
	@GetMapping("/page")
	public R<IPage<TaskPlanVO>> selectTaskPlanPage(TaskPlanPageQueryDTO pageQuery, Query query){
		return R.data(taskPlanService.selectTaskPlanPage(pageQuery, Condition.getPage(query)));
	}

	/**
	 * 任务计划下拉选择
	 * @return
	 */
	@TenantIgnore
	@GetMapping("/select/down")
	public R<List<TaskPlanVO>> getTaskPlanSelectDown(){
		return R.data(taskPlanService.getTaskPlanSelectDown());
	}

	/**
	 * 任务计划详情
	 * @param id
	 * @return
	 */
	@TenantIgnore
	@GetMapping("/detail")
	public R<TaskPlanVO> selectTaskPlanDetail(Long id){
		return R.data(taskPlanService.selectTaskPlanDetail(id));
	}

	/**
	 * 新增任务计划
	 * @param taskPlanSaveDTO
	 * @return
	 */
	@PostMapping("/save")
	public R<Boolean> saveTaskPlan(@RequestBody TaskPlanSaveDTO taskPlanSaveDTO){
		return R.status(taskPlanService.saveTaskPlan(taskPlanSaveDTO));
	}

	/**
	 * 修改任务计划
	 * @param id
	 * @param taskPlanSaveDTO
	 * @return
	 */
	@PutMapping("/{id}")
	public R<Boolean> updateTaskPlan(@PathVariable Long id, @RequestBody TaskPlanSaveDTO taskPlanSaveDTO){
		return R.status(taskPlanService.updateTaskPlan(id,taskPlanSaveDTO));
	}

	/**
	 * 删除任务计划
	 * @param ids
	 * @return
	 */
	@DeleteMapping("/remove")
	public R<Boolean> removeTaskPlan(String ids){
		return R.status(taskPlanService.removeByIds(Func.toLongList(ids)));
	}

	/**
	 * 复制任务计划及关联模板关系
	 * @param taskPlan
	 * @return
	 */
	@TenantIgnore
	@PostMapping("/copy")
	public R<Boolean> copyTaskPlan(@RequestBody MaintainTaskPlan taskPlan){
		return R.status(taskPlanService.copyTaskPlan(taskPlan.getId()));
	}

	/**
	 * 获取分页查询条件
	 * @return
	 */
	@TenantIgnore
	@GetMapping("/query/condition")
	public R<?> queryCondition(){

		HashMap<String,Object> resultMap = new HashMap<>(3);

		// 封装创建租户条件
		List<String> tenantIds = new ArrayList<>();
		tenantIds.add(BladeConstant.ADMIN_TENANT_ID);
		tenantIds.add(AuthUtil.getTenantId());
		List<Tenant> tenantList = tenantService.list(Wrappers.<Tenant>lambdaQuery().in(Tenant::getTenantId,tenantIds));
		resultMap.put("tenantList",tenantList);

		// 封装适用梯型条件
		List<Dict> applyElevatorTypeList = DictCache.getList("apply_elevator_type");
		resultMap.put("applyElevatorTypeList",applyElevatorTypeList);

		// 封装任务类型条件
		List<Dict> planTypeList = DictCache.getList("plan_type");
		resultMap.put("planTypeList",planTypeList);

		// 封装工作项位置条件
		List<Dict> locationTypeList = DictCache.getList("location_type");
		resultMap.put("locationTypeList",locationTypeList);

		// 封装年检类型条件
		List<Dict> asTypeList = DictCache.getList("as_type");
		resultMap.put("asTypeList",asTypeList);

		return R.data(resultMap);
	}

}
